var fs = require('fs');
var dialog = require('electron').remote.dialog;
var app = require('electron').remote.app;
var dialogEX = Require('./component/_dialog.js');
module.exports = function () {
    dialog.showSaveDialog({
        title: '导出接口文档',
        defaultPath: app.getPath('desktop'),
        filters: [
            { name: 'csv', extensions: ['csv'] }
        ],
    }, (path) => {
        __DBObject__.list.getAll(function (error, docs) {
            var data = {};
            for (let i = 0; i < docs.length; i++) {
                const item = docs[i];
                data[item.id] = {
                    id: item.id,
                    title: item.title,
                    parentId: item.parentId,
                    // requestData: item.requestData,
                    requestURL: item.requestURL ? item.requestURL.replace('<div>', '').replace('</div>\n', '') : '',
                    contentIndex: item.contentIndex,
                    contentType: item.contentType,
                    contentData: item.requestType == 'json' ? (item.requestData ? item.requestData : '') : item.formData,
                    type: item.requestType == 'form' ? '表单' : 'json',
                };
            };
            var Json = []
            for (const key in data) {
                if (data.hasOwnProperty(key)) {
                    const row = data[key];
                    if (row.contentType != 'folder') {
                        var _data = {};
                        var p1 = row;
                        var p2 = data[row.parentId];
                        if (p2) {
                            // console.log(p2.contentIndex, p2.parentId);
                            var p3 = p2.parentId ? data[p2.parentId] : '';
                            if (p3) {
                                _data.number = p3.contentIndex + '.' + p2.contentIndex + '.' + p1.contentIndex;
                                _data.parentText = p3.title + '.' + p2.title;
                            } else {
                                _data.number = p2.contentIndex + '.' + p1.contentIndex;
                                _data.parentText = p2.title;
                            };
                            _data.title = row.title;
                            _data.requestURL = row.requestURL;
                            _data.contentType = row.contentType;
                            _data.contentData = row.contentData;
                            _data.type = row.type;
                            Json.push(_data);
                        } else {
                            _data.number = p1.contentIndex;
                            _data.parentText = ' ';
                            _data.title = row.title;
                            _data.requestURL = row.requestURL;
                            _data.contentType = row.contentType;
                            _data.contentData = row.contentData;
                            _data.type = row.type;
                            Json.push(_data);
                        };
                    };
                }
            };
            var csv = '';
            for (let i = 0; i < Json.length; i++) {
                const item = Json[i];
                var string = item.number + ',' + item.parentText + ',' + item.title + ',' + item.contentType + ',' + item.requestURL + ',' + item.type + ',' + item.contentData.replace(/\,/g, '，').replace(/[\r\n]/g, "") + '\n';
                csv += string;
            };
            fs.writeFile(path, csv, function (err) {
                if (err) {
                    dialogEX({
                        title: '警告',
                        body: '<div style="padding:20px;">读写文件失败</div>',
                        buttons: [{
                            text: '确定',
                            event: (dialogEvent) => {
                                dialogEvent.close();
                            },
                        }],
                        width: 400,
                        height: 200
                    });
                } else {
                    dialogEX({
                        title: '提示',
                        body: '<div style="padding:20px;">操作成功</div>',
                        buttons: [{
                            text: '确定',
                            event: (dialogEvent) => {
                                dialogEvent.close();
                            },
                        }],
                        width: 400,
                        height: 200
                    });
                };
            });
        });
    })
};